package Tests;

import org.junit.jupiter.api.*;
import org.openqa.selenium.By;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class ForumDetailTest extends AutoTestUtils{
    @BeforeAll
    public static void setUpBeforeClass() throws Exception {
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //打开论坛首页页面
        webDriver.get("http://127.0.0.1:9580/index.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

    }
    /*
     *删除帖子
     */
    @Order(1)
    @Test
    void DeleteForum() throws InterruptedException {
        //进入我的帖子
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown > a")).click();
        webDriver.findElement(By.cssSelector("#index_user_profile")).click();
        //点击要删除的帖子"自动化测试"
        webDriver.findElement(By.cssSelector("#profile_article_body > li:nth-child(1) > div.card.timeline-event-card > div > div > div > div > div.text-truncate > a")).click();
        //点击删除
        webDriver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div:nth-child(1) > div.col-9.card.card-lg > div.card-footer.bg-transparent.mt-auto.justify-content-end > div > div:nth-child(3) > div > a")).click();
        sleep(3000);
        //确认删除
        webDriver.findElement(By.cssSelector("#details_artile_delete")).click();

    }

    /*
     *检验是否删除成功
     */
    @Order(2)
    @Test
    void DeleteSuccess(){
        webDriver.get("http://127.0.0.1:9580/index.html");
        //进入我的帖子
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown > a")).click();
        webDriver.findElement(By.cssSelector("#index_user_profile")).click();
        //获取第一条帖子标题
        String cur_title = webDriver.findElement(By.xpath("//*[@id=\"profile_article_body\"]/li[1]/div[2]/div/div/div/div/div[1]/a")).getText();
        //获取第一条帖子发布时间
        String cur_time = webDriver.findElement(By.xpath("//*[@id=\"profile_article_body\"]/li[1]/div[2]/div/div/div/div/div[2]/div/div[1]/ul/li")).getText();
        //查看第一条帖子标题和发布时间是否与删除的不一致
        Assertions.assertNotEquals("自动化测试", cur_title);
        Date date = new Date();
        SimpleDateFormat this_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Assertions.assertNotEquals(this_time, cur_time);

    }
}
